﻿@{
    ViewData["Title"] = "添加权限";
}
<style type="text/css">
    .layui-unselect dl {
        max-height: 200px;
    }
</style>
<div class="layui-fluid">
    <div class="layui-card layui-form" lay-filter="layuiadmin-form-list" id="layuiadmin-form-list" style="padding: 20px 30px 0 0;">
        <div class="layui-form-item">
            <label for="PmsCode" class="layui-form-label">
                <span class="x-red">*</span>权限代码
            </label>
            <div class="layui-input-inline">
                <input type="text" id="PmsCode" name="PmsCode" lay-verify="required"
                       autocomplete="off" class="layui-input">
                <input type="hidden" name="Id" />
            </div>
            <div class="layui-form-mid layui-word-aux">
                <span class="x-red">*</span>
            </div>
        </div>
        <div class="layui-form-item">
            <label for="phone" class="layui-form-label">
                <span class="x-red">*</span>权限名称
            </label>
            <div class="layui-input-inline">
                <input type="text" id="PmsName" name="PmsName" autocomplete="off" lay-verify="required" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux">
                <span class="x-red">*</span>
            </div>
        </div>
        <div class="layui-form-item">
            <label for="PmsType" class="layui-form-label">
                <span class="x-red">*</span>权限类别
            </label>
            <div class="layui-input-inline">
                <select lay-filter="PmsType" id="PmsType" name="PmsType" lay-verify="required">
                    <option value="">--请选择类别--</option>
                    <option value="m">模块</option>
                    <option value="m1">一级目录</option>
                    <option value="m2">二级目录</option>
                    <option value="c">菜单</option>
                    <option value="p">权限</option>
                </select>
            </div>
            <div class="layui-form-mid layui-word-aux">
                <span class="x-red">*</span>
            </div>
        </div>
        <div class="layui-form-item">
            <label for="ParentId" class="layui-form-label">
                父权限
            </label>
            <div class="layui-input-inline">
                <select id="ParentId" name="ParentId" lay-search=""></select>
            </div>
        </div>
        <div id="addrpnl" class="layui-form-item">
            <label for="PmsAddr" class="layui-form-label">
                <span class="x-red">*</span>权限地址
            </label>
            <div class="layui-input-block">
                <input type="text" id="PmsAddr" name="PmsAddr" size="60"
                       autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label for="IconStr" class="layui-form-label">
                <span class="x-red">*</span>图标
            </label>
            <div class="layui-input-inline">
                <input type="text" id="IconStr" name="IconStr" size="60"
                       autocomplete="off" class="layui-input"><i class="iconfont"></i>
            </div>
        </div>
        <div class="layui-form-item">
            <label for="PmsDesc" class="layui-form-label">
                <span class="x-red">*</span>权限描述
            </label>
            <div class="layui-input-block">
                <textarea rows="3" cols="40" name="PmsDesc" id="PmsDesc" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <label for="Sort" class="layui-form-label">
                <span class="x-red">*</span>排序
            </label>
            <div class="layui-input-block">
                <input type="text" id="Sort" name="Sort" size="60" required=""
                       autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item" style="text-align:center;">
            <button class="layui-btn" lay-submit lay-filter="layuiadmin-form-submit" id="layuiadmin-form-submit">立即提交</button>
            <button class="layui-btn layui-btn-danger" type="button" onclick="onClose()">
                关闭
            </button>
        </div>
    </div>
</div>
<script type="text/javascript">
    var layer, selflowIns;
    layui.config({
        base: '/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'form', 'layer'], function () {
        var $ = layui.$
            , form = layui.form
            , admin = layui.admin;
        var layer = layui.layer;
        
        form.render(null, 'layuiadmin-form-list');
        //监听提交
        form.on('submit(layuiadmin-form-submit)', function (data) {
            var field = data.field; //获取提交的字段
            admin.req({
                url: '/Pms/Save'
                , method: 'post'
                , data: field
                , done: function (res) {
                    layer.msg(res.msg, {
                        offset: '50px'
                        , icon: 1
                        , time: 2000
                    }, function () {
                        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.InitPms();
                        //parent.layui.table.reload('LAY-dict-list'); //重载表格
                        parent.layer.close(index); //再执行关闭
                    });
                }
            });
        });
        init();
    })

    function onClose() {
        var index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(index);
    }

    function init() {
        InitParentPms();
        $("#IconStr").blur(function () {
            $(".iconfont").html($(this).val());
        });
        var Id ='@Request["Id"]';
        var ParentId ='@Request["ParentId"]';
        if (Id != undefined && Id != "") {
            layui.admin.req({
                url: '/Pms/Get'
                , method: 'post'
                , data: "Id=" + Id
                , done: function (res) {
                    initForm("layuiadmin-form-list", res.data);
                    layui.form.render(null, 'layuiadmin-form-list');
                }
            });
        }
        if (ParentId != undefined && ParentId != "") {
            $("#ParentId").val(ParentId);
        }
        layui.form.render(null, 'layuiadmin-form-list');
    }
    //初始化父权限
    function InitParentPms() {
        layui.admin.req({
            url: '/Pms/GetsParentPms'
            , method: 'post'
            , async: false
            , done: function (res) {
                $("#ParentId").find("option").remove();
                $("#ParentId").append(" <option value=''>--请选择--</option>");
                for (var i = 0; i < res.data.length; i++) {
                    $("#ParentId").append(" <option value='" + res.data[i].Id + "'>" + res.data[i].PmsName + "</option>");
                }
                layui.form.render("select", 'layuiadmin-form-list');
            }
        });

        var ParentId = '@Request["ParentId"]';
        if (ParentId != undefined && ParentId != "") {
            $("#ParentId").val(ParentId);
            layui.form.render("select", 'layuiadmin-form-list');
        }
    }
</script>
